{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python Path\n",
    "Before getting started it is important to add OpenVDS to the Python path. This can be done by installing OpenVDS as a Python Wheel or as a Python Egg. It's is possible to use a local build. When doing this it's important that the version of Python running the Jupyter Notebook kernels is the same version used to build OpenVDS. In Jupyter Notebook the About dialog under the Help menu will give you the Python version run by the Jupyter Notebook kernels.\n",
    "\n",
    "For OpenVDS there is the OPENVDS_PYTHON_VERSION CMake variable that can be set to pick up a Python of desired version from the system.\n",
    "\n",
    "The notebook will give errors if OpenVDS is not in the Python path.\n",
    "The error will complain about ModuleNotFoundError:\n",
    "\n",
    "`ModuleNotFoundError: No module named 'openvds'`\n",
    "\n",
    "An example of how to import a local build on a Windows system would be to use the following two script lines: (Note that this is not needed if OpenVDS is in the PYTHONPATH environment variable or OpenVDS is installed as a Wheel or Egg)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#This is not needed if OpenVDS is in the python path!\n",
    "import sys\n",
    "sys.path.append(r'C:\\Projects\\open-vds\\out\\build\\x64-Debug\\python')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then OpenVDS can be imported"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openvds\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import matplotlib.image as mpimg\n",
    "import numpy as np\n",
    "import time\n",
    "%matplotlib inline\n",
    "matplotlib.rcParams['figure.figsize'] = (16, 16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Open a VDS connection and get the Layout object for the connection\n",
    "Specific OpenOptions can to be used to open a connection to different cloud vendors. The AWS open options also relies on the AWS standardized credentials file in $HOME/.aws/credentials\n",
    "\n",
    "Alternativly a url and connection string pair can be used. This scheme is descibed here:\n",
    "http://osdu.pages.community.opengroup.org/platform/domain-data-mgmt-services/seismic/open-vds/connection.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#openOptions = openvds.AWSOpenOptions(\"bucket\", \"folder_in_bucket\", \"region\")\n",
    "#openOptions = openvds.AzureOpenOptions(\"connection_string\", \"container\", \"blob\") #blob is ignored for now\n",
    "#openOptions = openvds.GoogleOpenOptions(\"bucket\", \"folder_in_bucket\")\n",
    "#vds = openvds.open(openOptions)\n",
    "vds = openvds.open(\"some url\", \"optional connection string\")\n",
    "layout = openvds.getLayout(vds)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Layout\n",
    "The layout can be used to retrieve properties about the VDS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"ChannelCount: {}\".format(layout.getChannelCount()))\n",
    "for i in range(layout.getChannelCount()):\n",
    "    print(\"  Channel Name: {}\".format(layout.getChannelName(i)))\n",
    "    print(\"    Value range: {} - {}\".format(layout.getChannelValueRangeMin(i), layout.getChannelValueRangeMax(i)))\n",
    "    \n",
    "print(\"Dimensionality: {}\".format(layout.getDimensionality()))\n",
    "for i in range(layout.getDimensionality()):\n",
    "    print(\"  Dimension name: {}\".format(layout.getDimensionName(i)))\n",
    "    print(\"    Number of samples: {}\".format(layout.getDimensionNumSamples(i)))\n",
    "    print(\"    Coordinate min max {} - {}\".format(layout.getDimensionMin(i), layout.getDimensionMax(i)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Metadata accessor\n",
    "Metadata can be accessed through the layout objects getMetadataXX function. The type is part of the key and no implicit conversion will occur.\n",
    "- getMetadataInt\n",
    "- getMetadataIntVector2\n",
    "- getMetadataIntVector3\n",
    "- getMetadataIntVector4\n",
    "- getMetadataFloat\n",
    "- getMetadataFloatVector2\n",
    "- getMetadataFloatVector3\n",
    "- getMetadataFloatVector4\n",
    "- getMetadataDouble\n",
    "- getMetadataDoubleVector2\n",
    "- getMetadataDoubleVector3\n",
    "- getMetadataDoubleVector4\n",
    "- getMetadataString\n",
    "- getMetadataBLOB\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MetaType = openvds.MetadataType\n",
    "if layout.isMetadataDoubleVector2Available(\"SurveyCoordinateSystem\", \"Origin\"):\n",
    "    print(\"SurveyCoordinateSystem::Origin: {}\".format(layout.getMetadataDoubleVector2(\"SurveyCoordinateSystem\", \"Origin\")))\n",
    "if layout.isMetadataDoubleVector2Available(\"SurveyCoordinateSystem\", \"InlineSpacing\"):\n",
    "    print(\"SurveyCoordinateSystem::InlineSpacing: {}\".format(layout.getMetadataDoubleVector2(\"SurveyCoordinateSystem\", \"InlineSpacing\")))\n",
    "if layout.isMetadataDoubleVector2Available(\"SurveyCoordinateSystem\", \"CrosslineSpacing\"):\n",
    "    print(\"SurveyCoordinateSystem::CrosslineSpacing: {}\".format(layout.getMetadataDoubleVector2(\"SurveyCoordinateSystem\", \"CrosslineSpacing\")))\n",
    "#layout.getMetadata(\"SEGY\", \"TextHeader\", openvds.core.MetadataType.BLOB)\n",
    "#layout.getMetadata(\"SEGY\", \"BinaryHeader\", openvds.core.MetadataType.BLOB)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The AccessManager api\n",
    "The AccessManager api can be used to retieve data from the vds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accessManager = openvds.VolumeDataAccessManager(vds)\n",
    "axisDescriptors = [layout.getAxisDescriptor(dim) for dim in range(layout.getDimensionality())]\n",
    "sliceType = 'inline'\n",
    "sliceIndex = 55\n",
    "sliceDimension = 2 if sliceType == 'inline' else 1 if sliceType == 'crossline' else 0 if sliceType == 'timeslice' else 0 if sliceType == 'depthslice' else -1\n",
    "\n",
    "min = tuple(sliceIndex + 0 if dim == sliceDimension else 0 for dim in range(6))\n",
    "max = tuple(sliceIndex + 1 if dim == sliceDimension else layout.getDimensionNumSamples(dim) for dim in range(6))\n",
    "\n",
    "req = accessManager.requestVolumeSubset(min, max, format = openvds.VolumeDataChannelDescriptor.Format.Format_R32)\n",
    "height = max[0] if sliceDimension != 0 else max[1]\n",
    "width  = max[2] if sliceDimension != 2 else max[1]\n",
    "\n",
    "data = req.data.reshape(width, height).transpose()\n",
    "cmap=plt.get_cmap(\"seismic\")\n",
    "plt.set_cmap(cmap)\n",
    "plt.imshow(data)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
